package pvn.com.controls.toolbar;

import pvn.com.screens.TitleScreen;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.BorderFactory;

public class BitmapButtonField extends ButtonField {
    /** Ảnh khi normal*/
    Bitmap mNormal;
    /** Ảnh khi focus*/
    Bitmap mFocused;
    /** Ảnh khi active*/
    Bitmap mActive;
    /** button width*/
    int mWidth;
    /** button height*/
    int mHeight;
    /** trạng thái acticve?*/
    public boolean isActive = false;
    /** hint của button*/
    public String hint = "";
    public int tag = 0;

    /**
     * khởi tạo bitmap button field
     * @param normal - ảnh khi normal
     * @param active - Ảnh khi active
     * @param focused - Ảnh khi focus
     * @param _hint - hint của button
     * @param _tag
     */
    public BitmapButtonField(Bitmap normal, Bitmap active, Bitmap focused, String _hint, int _tag) {
        this(0L, normal, active, focused, _hint, _tag);
    }

    /**
     * Khởi tạo bitmap button field
     * @param style
     * @param normal - ảnh khi normal
     * @param active - Ảnh khi active
     * @param focused - Ảnh khi focus
     * @param _hint - hint của button
     * @param _tag
     */
    public BitmapButtonField(long style, Bitmap normal, Bitmap active, Bitmap focused,
            String _hint, int _tag) {
        super(0x10000 | style);
        this.mNormal = normal;
        this.mFocused = focused;
        this.mActive = active;
        this.mWidth = (this.mNormal.getWidth() + 6);
        this.mHeight = (this.mNormal.getHeight() + 6);
        this.hint = _hint;
        this.tag = _tag;
        setMargin(0, 0, 0, 0);
        setPadding(0, 0, 0, 0);
        //tạo border
        setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        //tạo border cho truong hop active
        setBorder(Field.VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(Field.VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(Field.VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBackground(BackgroundFactory.createBitmapBackground(Bitmap
                .getBitmapResource("images/toolbar/bg.png")));
    }

    /* (non-Javadoc)
     * vẽ ra màn hình
     * @see net.rim.device.api.ui.component.ButtonField#paint(net.rim.device.api.ui.Graphics)
     */
    protected void paint(Graphics graphics) {
        Bitmap bitmap = null;
        switch (getVisualState()) {
        case Field.VISUAL_STATE_NORMAL:
            if (this.isActive)
                bitmap = this.mActive;
            else
                bitmap = this.mNormal;
            break;
        case Field.VISUAL_STATE_ACTIVE:
        case Field.VISUAL_STATE_FOCUS:
            bitmap = this.mFocused;
            break;
        default:
            bitmap = this.mNormal;
        }
        graphics.drawBitmap(0, (this.mHeight - bitmap.getHeight()) / 2, bitmap.getWidth(),
                bitmap.getHeight(), bitmap, 0, 0);
    }

    protected void applyTheme(Graphics g, boolean b) {
    }

    protected void applyTheme() {
    }

    protected void drawFocus(Graphics graphics, boolean on) {
    }

    public int getPreferredWidth() {
        return this.mWidth;
    }

    public int getPreferredHeight() {
        return this.mHeight;
    }

    protected void layout(int width, int height) {
        setExtent(this.mWidth, this.mHeight);
    }

    /* (non-Javadoc)
     * hiển thị hint khi focus tới
     * @see net.rim.device.api.ui.Field#onFocus(int)
     */
    protected void onFocus(int direction) {
        super.onFocus(direction);
        TitleScreen parentScreen = (TitleScreen) getScreen();
        parentScreen.ShowTooltip(this.hint, getLeft(), 76, 3000);
    }

    protected void onUnfocus() {
        super.onUnfocus();
        TitleScreen parentScreen = (TitleScreen) getScreen();
        parentScreen.HideTooltip();
    }
}
